﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RioStudio.Website.Model;

namespace RioStudio.Website.View.Gallery
{
    public partial class AlbumDetail : RioPage
    {
        public string Slug { get; set; }

        public string Image { get; set; }

        public string Description { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["slug"] != null)
            {
                this.Slug = Request.Params["slug"];
            }
            else
            {
                Response.Redirect("~/Album");
            }

            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Slug))
                {
                    if (HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        txtFullname.Text = User.Identity.Name;
                    }


                    var albumnImage = tData.Instance.ExecuteReader<RioStudio.Website.Model.Gallery>("Proc_GetGalleryBySlug", Slug).FirstOrDefault();

                    if (albumnImage != null)
                    {
                        //imgFirstImage.ImageUrl = "/ImageHandler.axd?image=" + ResolveUrl(albumnImage.GalleryAvatar) + "&width=549&height=337";
                        imgFirstImage.ImageUrl = ResolveUrl(albumnImage.GalleryAvatar);
                        this.Image = ResolveUrl(albumnImage.GalleryAvatar);
                        this.Description = albumnImage.Description;
                        lnkFirstImage.HRef = ResolveUrl(albumnImage.GalleryAvatar);
                        hiddenImageID.Value = albumnImage.GalleryID.ToString();
                    }

                    rlvComment.Rebind();

                    rlvAnotherImage.Rebind();

                    var gallery = tData.Instance.ExecuteReader<RioStudio.Website.Model.Gallery>("Proc_GetGalleryBySlug", Slug).FirstOrDefault();

                    if (gallery != null)
                    {
                        lblGalleryPlace.Text = gallery.GalleryPlace;
                        lblGalleryName.Text = gallery.GalleryName;
                    }

                    BindingFeaturedAlbum();
                }
            }
        }

        private void BindingFeaturedAlbum()
        {
            dtlAnotherAlbum.DataSource = tData.Instance.ExecuteReader<RioStudio.Website.Model.Gallery>("Proc_GetAllGalleryIsFeature", true);
            dtlAnotherAlbum.DataBind();
        }

        protected void btnSend_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                var model = new GalleryComment();
                model.GalleryCommentID = Guid.NewGuid();
                model.GalleryID = new Guid(hiddenImageID.Value);
                model.Detail = txtDetail.Text;
                model.PostedName = txtFullname.Text;
                model.IsApproved = false;

                tData.Instance.ExecuteNonQuery<GalleryComment>("Proc_InsertGalleryComment", model);

                txtDetail.Text = string.Empty;
                txtFullname.Text = string.Empty;

                rlvComment.Rebind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void rlvComment_NeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
        {
            if (!string.IsNullOrEmpty(hiddenImageID.Value))
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    rlvComment.DataSource = tData.Instance.ExecuteReader<GalleryComment>("Proc_GetAllGalleryCommentByGalleryID", new Guid(hiddenImageID.Value), null);
                }
                else
                {
                    rlvComment.DataSource = tData.Instance.ExecuteReader<GalleryComment>("Proc_GetAllGalleryCommentByGalleryID", new Guid(hiddenImageID.Value), true);
                }
            }
        }

        protected void rlvAnotherImage_OnNeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
        {
            var lstImage = tData.Instance.ExecuteReader<GalleryImage>("Proc_GetGalleryImageByAlbumSlug", Slug);

            if (lstImage.Count > 0)
            {
                rlvAnotherImage.DataSource = lstImage;


            }

            //if (!string.IsNullOrEmpty(hiddenImageID.Value))
            //{
            //    if (HttpContext.Current.User.Identity.IsAuthenticated)
            //    {
            //        rlvComment.DataSource = tData.Instance.ExecuteReader<GalleryComment>("Proc_GetAllGalleryCommentByGalleryID", new Guid(hiddenImageID.Value), null);
            //    }
            //    else
            //    {
            //        rlvComment.DataSource = tData.Instance.ExecuteReader<GalleryComment>("Proc_GetAllGalleryCommentByGalleryID", new Guid(hiddenImageID.Value), true);
            //    }
            //}
        }

        //rlvAnotherImage_OnNeedDataSource

        protected void rlvComment_ItemCommand(object sender, Telerik.Web.UI.RadListViewCommandEventArgs e)
        {
            try
            {

                var _imageCommentID = Guid.Empty;
                var _imageComment = new GalleryComment();
                switch (e.CommandName)
                {
                    case "Remove":
                        _imageCommentID = new Guid(e.CommandArgument.ToString());
                        _imageComment.GalleryCommentID = _imageCommentID;
                        tData.Instance.ExecuteNonQuery<GalleryComment>("Proc_DeleteGalleryComment", _imageComment);
                        break;
                    case "Approve":
                        _imageCommentID = new Guid(e.CommandArgument.ToString());
                        _imageComment.GalleryCommentID = _imageCommentID;
                        _imageComment.IsApproved = true;
                        tData.Instance.ExecuteNonQuery<GalleryComment>("Proc_ChangeGalleryCommentStatus", _imageComment);
                        break;
                    case "Unapprove":
                        _imageCommentID = new Guid(e.CommandArgument.ToString());
                        _imageComment.GalleryCommentID = _imageCommentID;
                        _imageComment.IsApproved = false;
                        tData.Instance.ExecuteNonQuery<GalleryComment>("Proc_ChangeGalleryCommentStatus", _imageComment);
                        break;
                }

                rlvComment.Rebind();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}